$(function() {

    //get current webpage href
    if(document.location.href.indexOf('p=adv_manage') == -1) {
        $server_page = '?p=p_index';
    } else {
        $server_page = '?p=p_adv_manage';
    }

    //get total number of pages
    jQuery.post($server_page, { "type" : "getPageLength" }, function(data){
        // data obj holder
        window.p = {
            results: 10, //results per page
            active: false, //active page request?
            current: 1 //current page
        }

        //get number of pages
        p.total = Math.ceil( data / p.results );

        //show number of pages on screen (navigation)
        for(var $i=1; $i<=p.total; $i++) {
            $('.number_of_page > ul')
                .append('<li><a href="#"><div class="page_number">' + $i + '</div></a></li>');
        }

        $('.number_of_page > ul > li').click(function(e) {
            e.preventDefault();
            $current_page = ($('.number_of_page > ul > li').index(this)+1);
            //console.log('clicks' + $current_page);
            load_page_with_sort($sort_type, $current_page);
        });

        //load first page
        load_page_with_sort($sort_type, p.current);
    }, "html");



    $('.previous').click(function() {
        if( $current_page == 1 ) {
            return;
        }
        load_page_with_sort($sort_type, --$current_page);
    });

    $('.next').click(function() {
        if( $current_page == p.total ) {
            return;
        }
        load_page_with_sort($sort_type, ++$current_page);
    });

    $('.adv_sort_button').click(function() {
        $sort_type = $(this).index();
        //console.log('sort type:' + $sort_type);

        load_page_with_sort($sort_type, $current_page);

        $('.adv_sort_button').removeClass('selected');
        $(this).addClass(' selected');

    });


    //currency switch - changes $_SESSION SUPERGLOBAL on server
    $('.currency_switch_button').click(function() {
        console.log($(this).html());
        $.post("?p=ajax_requests", {
            "method" : 'ajax_change_currency',
            "curr" : $(this).html()
        }, function() {
            load_page_with_sort($sort_type, $current_page);
        });

        $('.currency_switch_button').removeClass('selected');
        $(this).addClass(' selected');
    });

});

var $current_page = 1, $sort_type = false, $server_page;

function load_page_with_sort($sort_type, number) {
    switch ($sort_type) {
        case 0:
            load_page(number, 'DESC', null);
            break;

        case 1:
            load_page(number, 'ASC', null);
            break;

        case 2:
            load_page(number, null, 'DESC');
            break;

        case 3:
            load_page(number, null, 'ASC');
            break;

        default :
            load_page(number, null, null);
    }
}

function load_page(number, price, date) {
    // Има ли активна заявка
    if(p.active) return; p.active = true;
    //console.log('price var: ' + price + ' //date var: ' + date);

    // Слагаме картинката за зареждане
    $("#adv_listings").html("<img src='img/spinner.gif' alt='' />");

    //console.log(number + ' ' + p.results);

    //Зареждаме страницата
    jQuery.post($server_page, { "page" : number, "howMuch":  p.results, "price": price, "date": date}, function(data){
        // Показваме резултатите
        $("#adv_listings").html(data);

        $('.page_number').css('background', '#EE684D');
        $('.page_number:eq(' + (number-1) + ')').css('background', 'black');

        // Вече няма активни заявки
        p.active = false;
    }, "html");
}
